import 'package:shared_preferences/shared_preferences.dart';

class SPUtil {
  static SharedPreferences? sharedPreferences;

  SPUtil._();

  static final SPUtil _instance = SPUtil._();

  static SPUtil get instance {
    return _instance;
  }

  Future init() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }

  static const String email = "email"; // 用户邮箱
  static const String messages = "messages"; // 用户本地消息

  static deleteHistory() async {
    sharedPreferences?.remove(messages);
  }

  static logout() async {
    sharedPreferences?.remove(email);
  }

  static dynamic get(dynamic key) {
    dynamic result;
    if (key.runtimeType == String) {
      result = sharedPreferences?.getString(key) ?? '';
    }
    if (key.runtimeType == bool) {
      result = sharedPreferences?.getBool(key) ?? false;
    }
    if (key.runtimeType == double) {
      result = sharedPreferences?.getDouble(key) ?? 0.0;
    }
    if (key.runtimeType == int) {
      result = sharedPreferences?.getInt(key) ?? 0;
    }
    return result;
  }

  static set(String key,dynamic value) {
    if (key.runtimeType == String) {
      sharedPreferences?.setString(key, value);
    }
    if (key.runtimeType == bool) {
      sharedPreferences?.setBool(key, value);
    }
    if (key.runtimeType == double) {
      sharedPreferences?.setDouble(key, value);
    }
    if (key.runtimeType == int) {
      sharedPreferences?.setInt(key, value);
    }
  }
}
